package com.leizi.igym.job.once;

import cn.hutool.core.collection.CollectionUtil;
import com.leizi.igym.dao.LostESDao;
import com.leizi.igym.dao.PlanESDao;
import com.leizi.igym.domain.Lost;
import com.leizi.igym.domain.Plan;
import com.leizi.igym.dto.LostESDTO;
import com.leizi.igym.service.PlanService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-23  19:46
 * @Description:
 */

@Slf4j
@Component
public class SelectPlanToES implements CommandLineRunner {

    @Resource
    private PlanService planService;

    @Resource
    private PlanESDao planESDao;

    @Override
    public void run(String... args) throws Exception {
        List<Plan> planList = planService.list();
        if (CollectionUtil.isEmpty(planList)) {
            return;
        }
        final int pageSize = 500;
        int total = planList.size();
        log.info("SelectPlanToES start, total {}", total);
        for (int i = 0; i < total; i += pageSize) {
            int end = Math.min(i + pageSize, total);
            log.info("sync from {} to {}", i, end);
            planESDao.saveAll(planList.subList(i, end));
        }
        log.info("SelectPlanToES end, total {}", total);
    }
}
